var a = getApp(), t = require("../../../utils/address-parse.js"), e = void 0;

Page({
    data: {
        address: {},
        textarea: ""
    },
    onLoad: function(a) {
        (e = this).initLoad(a);
    },
    smartInput: function(a) {
        this.setData({
            textarea: a.detail.value
        });
    },
    doParse: function() {
        var d = t(this.data.textarea);
        console.log(d), wx.showLoading({
            title: "解析中",
            mask: !0
        }), a.com.post("wx/location/parse", {
            address: d.city + d.area + d.addr
        }, function(a) {
            wx.hideLoading(), 0 === a.errno && e.setData({
                "address.latitude": a.data.location.lat,
                "address.longitude": a.data.location.lng,
                "address.street_number": a.data.address_components.street_number,
                "address.formatted_addresse": d.addr,
                "address.province": d.province,
                "address.city": d.city,
                "address.district": d.area,
                "address.phone": d.mobile,
                "address.name": d.name
            });
        });
    },
    initLoad: function(a) {
        e.setData({
            type: a.type,
            text: a.text,
            address: a.address ? JSON.parse(a.address) : {},
            address_default: a.address_default || null,
            page: a.page || null
        }), wx.setNavigationBarTitle({
            title: a.text + "地址"
        });
    },
    bindInput: function(a) {
        var t = this.data.address;
        t[a.currentTarget.dataset.name] = a.detail.value, this.setData({
            address: t
        });
    },
    submit: function() {
        if (this.data.address.latitude) if (this.data.address.phone) {
            var t = "add";
            this.data.address.id && (t = "update"), wx.showLoading({
                title: "加载中",
                mask: !0
            }), this.data.address_default && (this.data.address.address_default = this.data.address_default), 
            a.com.post("wx/address/" + t, this.data.address, function(a) {
                if (wx.hideLoading(), 0 === a.errno) {
                    wx.showToast({
                        title: a.errmsg,
                        mask: !0,
                        duration: 1e3
                    }), e.data.address.id = a.data.id;
                    var t = getCurrentPages(), d = 1;
                    "address" == e.data.page && (d = 2);
                    var s = getCurrentPages()[t.length - d - 1];
                    e.data.address_default ? s.getHomeAndCompany() : "start" == e.data.type ? s.setData({
                        startAddress: e.data.address
                    }) : s.setData({
                        endAddress: e.data.address
                    }), setTimeout(function() {
                        wx.navigateBack({
                            delta: d
                        });
                    }, 1e3);
                }
            });
        } else wx.showToast({
            title: "请输入手机号",
            icon: "none"
        }); else wx.showToast({
            title: "请选择一个地址",
            icon: "none"
        });
    }
});